package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.*;
import com.mxpio.erp.common.equipment.service.*;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 点检计划明细
 * @Author: jeecg-boot
 * @Date:   2021-09-16
 * @Version: V1.0
 */
@Service
public class EquipmentCheckPlanDetailServiceImpl extends BaseServiceImpl<EquipmentCheckPlanDetail> implements EquipmentCheckPlanDetailService {


    @Autowired
    private EquipmentCheckDetailService equipmentCheckDetailService;
    @Autowired
    private EquipmentCheckDetailParamService equipmentCheckDetailParamService;
    @Autowired
    private EquipmentCheckPlanDetailParamService equipmentCheckPlanDetailParamService;
    @Autowired
    private EquipmentCommParametersDetailService equipmentCommParametersDetailService;

	@Override
    @Transactional
	public List<EquipmentCheckPlanDetail> selectByMainId(String mainId) {
        Criteria c = Criteria.create().addCriterion("planId", Operator.EQ, mainId);
		return list(EquipmentCheckPlanDetail.class,c);
	}

	@Transactional(rollbackFor = Exception.class)
    @Override
    public void addDetailByCategory(String planId, String categoryId) {

	    // 查询分类下的明细，然后保存
        List<EquipmentCheckDetail> checkDetails = equipmentCheckDetailService.selectByMainId(categoryId);
        checkDetails.forEach(categoryDetail -> {
            EquipmentCheckPlanDetail planDetail = new EquipmentCheckPlanDetail();
            BeanUtils.copyProperties(categoryDetail,planDetail,"id");
            planDetail.setLowerLimit(categoryDetail.getLowerLimit());
            planDetail.setUpperLimit(categoryDetail.getUpperLimit());
            planDetail.setDetailId(categoryDetail.getId());
            planDetail.setPlanId(planId);
            save(planDetail);

            // 处理计划明细中的选项数据
            if (planDetail.getChooseEnable() == 1) {
                List<EquipmentCheckDetailParam> checkDetailParams = equipmentCheckDetailParamService.selectByMainId(categoryDetail.getId());
                List<EquipmentCheckPlanDetailParam> checkPlanDetailParams = checkDetailParams.stream().map(categoryParam -> {
                    EquipmentCheckPlanDetailParam planDetailParam = new EquipmentCheckPlanDetailParam();
                    BeanUtils.copyProperties(categoryParam, planDetailParam,"id","createTime","createBy","updateBy","updateTime","createDept");
                    planDetailParam.setDetailId(planDetail.getId());
                    return planDetailParam;
                }).collect(Collectors.toList());
                equipmentCheckPlanDetailParamService.save(checkPlanDetailParams);
            }
        });

    }
	@Transactional(rollbackFor = Exception.class)
    @Override
    public void addDetail(EquipmentCheckPlanDetail equipmentCheckPlanDetail) {
        save(equipmentCheckPlanDetail);

        // 处理计划明细中的选项数据
        if (equipmentCheckPlanDetail.getChooseEnable() == 1) {
            List<EquipmentCommParametersDetail> equipmentCommParametersDetails = equipmentCommParametersDetailService.selectByMainId(equipmentCheckPlanDetail.getParamId());
            List<EquipmentCheckPlanDetailParam> checkPlanDetailParams = equipmentCommParametersDetails.stream().map(commParam -> {
                EquipmentCheckPlanDetailParam planDetailParam = new EquipmentCheckPlanDetailParam();
                BeanUtils.copyProperties(commParam, planDetailParam,"id","createTime","createBy","updateBy","updateTime","createDept");
                planDetailParam.setDetailId(equipmentCheckPlanDetail.getId());
                return planDetailParam;
            }).collect(Collectors.toList());
            equipmentCheckPlanDetailParamService.save(checkPlanDetailParams);
        }
    }
}
